/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","comm/datatable/datatable"],function($,$bb,$datatable){
	var servers = {};
	var URL = $bb.baseUrl("followapp");
    
    /**
     * 入参
     */
    var inParams = function(args){
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "channelType": "2",
                "evaluationType": "Neuropsychology",
                "userName":$.trim( $("#datatableForm input[name=userName]").val() ),
                "phoneNo":$.trim( $("#datatableForm input[name=phoneNo]").val() ),
                "beginTime":$("#datatableForm input[name=beginTime]").val(),
                "endTime":$("#datatableForm input[name=endTime]").val(),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsGender = function(e){
        return e.gender == 'F' ? "女": e.gender == 'M' ? "男" : "未知";
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":'userName'},
            {"data":'phoneNo'},
            {"data":columnsGender},
            {"data":'age'},
            {"data":'testTime'},
            {
                "data":null,
                "className":"operate"
            }
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        var h = '<div class="operate-box"><span class="color-green cursor-pointer assessList-new">新做测评</span><span class="color-green cursor-pointer ml8 assessList-history">历史测评</span></div>';
        $('td.operate', row).html(h);
        $("td.operate .assessList-new",row).unbind("click");
        $("td.operate .assessList-new",row).click(function(){
            delete data.result;
            $bb.openPage("cnbsrDetail",data);
        });
        $("td.operate .assessList-history",row).unbind("click");
        $("td.operate .assessList-history",row).click(function(){
            $bb.openPage("cnbsrHistoryList",data);
        });
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        var id = $(".layout-bottom table").attr("id");
        $("#" + id).prev("div.row").remove();
        // 搜索 
        $("#serchBtn").click(function(){
            $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatable.table.api().ajax.params();   //获取dt请求参数 
        });
        // 初始化所有工具提示
        $(function(){$("[data-toggle='tooltip']").tooltip();});
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "service/evaluation/recordUserlist?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

	return servers;
});